package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class ProjectDetailServlet extends HttpServlet {

	public ProjectDetailServlet() {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		List<Project> plist = null;
		JSONArray jsonProject = null;

		int pno = Integer.parseInt(request.getParameter("pno"));
		System.out.println(pno);

		SelProject sp = new SelProject();
		plist = sp.getProjectDetail(pno);

		if (plist != null && plist.size() > 0) {
			Project p = (Project) plist.get(0);

			jsonProject = new JSONArray();
			JSONObject pnoData = new JSONObject();
			pnoData.put("name", "项目名称");
			pnoData.put("value", p.getPname());
			pnoData.put("group", "项目基本信息");
			jsonProject.add(pnoData);

			JSONObject stimeData = new JSONObject();
			stimeData.put("name", "创建时间");
			stimeData.put("value", p.getStime());
			stimeData.put("group", "项目基本信息");
			jsonProject.add(stimeData);

			JSONObject etimeData = new JSONObject();
			etimeData.put("name", "截止时间");
			etimeData.put("value", p.getEtime());
			etimeData.put("group", "项目基本信息");
			jsonProject.add(etimeData);

			JSONObject pubnameData = new JSONObject();
			pubnameData.put("name", "发布人");
			pubnameData.put("value", p.getPubname());
			pubnameData.put("group", "项目基本信息");
			jsonProject.add(pubnameData);

			String progress = "";
			if(p.getProgress().equals("1")) {
				progress = "已完成";
			} else {
				progress = "进行中";
			}
			JSONObject progressData = new JSONObject();
			progressData.put("name", "项目进度");
			progressData.put("value", progress);
			progressData.put("group", "项目基本信息");
			jsonProject.add(progressData);

			List<Map> memberList = p.getMemberList();
			String memberString = "";
			if(memberList != null && memberList.size() > 0) {
				for (int m = 0; m < memberList.size(); m++) {
					Map<String, String> map = memberList.get(m);
					String sname = map.get("sname");
					String stage = map.get("stage");
					if (m != 0) {
						memberString += ", " + sname;
					} else {
						memberString += sname;
					}
					
					String st = "";
					if(stage.equals("todo")) {
						st = "待接受";
					} else if(stage.equals("doing")) {
						st = "进行中";
					} else {
						st = "已完成";
					}
					JSONObject stageData = new JSONObject();
					stageData.put("name", sname);
					stageData.put("value", st);
					stageData.put("group", "参与者进度");
					jsonProject.add(stageData);
				}
	
				JSONObject partakerData = new JSONObject();
				partakerData.put("name", "项目参与人");
				partakerData.put("value", memberString);
				partakerData.put("group", "项目基本信息");
				jsonProject.add(partakerData);
				
				JSONObject pcontData = new JSONObject();
				pcontData.put("name", "项目描述");
				pcontData.put("value", p.getPcont());
				pcontData.put("group", "项目基本信息");
				jsonProject.add(pcontData);
			}
			
			List<Map> attachList = p.getAttachList();
			if(attachList != null && attachList.size() > 0) {
				for (int a = 0; a < attachList.size(); a++) {
					Map<String, Object> map = attachList.get(a);
					JSONObject attachData = new JSONObject();
					
					String value = (Integer)map.get("ano") + "," + (String)map.get("aname");
					attachData.put("name", "附件");
					attachData.put("value", value);
					attachData.put("group", "项目基本信息");
					jsonProject.add(attachData);
				}
			}

		}

		out.print(jsonProject);
		out.flush();
		out.close();
	}

}
